package com.sjgtw.cloud.web.path.service.impl;

import com.sjgtw.cloud.common.core.constant.CacheConstants;
import com.sjgtw.cloud.common.core.utils.DateUtils;
import com.sjgtw.cloud.common.core.utils.SecurityUtils;
import com.sjgtw.cloud.common.core.utils.bean.BeanUtils;
import com.sjgtw.cloud.common.redis.service.RedisService;
import com.sjgtw.cloud.system.api.model.LoginUser;
import com.sjgtw.cloud.web.path.domain.EmergencyPath;
import com.sjgtw.cloud.web.path.domain.PathPoint;
import com.sjgtw.cloud.web.path.mapper.EmergencyPathMapper;
import com.sjgtw.cloud.web.path.mapper.PathPointMapper;
import com.sjgtw.cloud.web.path.service.IEmergencyPathService;
import com.sjgtw.cloud.web.path.vo.EmergencyPathVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 应急路径Service业务层处理
 * 
 * @author lvyoufang
 * @date 2024-03-19
 */
@Service
public class EmergencyPathServiceImpl implements IEmergencyPathService 
{
    @Autowired
    private EmergencyPathMapper emergencyPathMapper;
    @Autowired
    private PathPointMapper pathPointMapper;

    @Autowired
    private RedisService redisService;

    /**
     * 查询应急路径
     * 
     * @param id 应急路径主键
     * @return 应急路径
     */
    @Override
    public EmergencyPathVO selectEmergencyPathById(Long id)
    {
        EmergencyPath path = emergencyPathMapper.selectEmergencyPathById(id);
        if (path == null) {
            return null;
        }

        EmergencyPathVO pathVO = new EmergencyPathVO();
        BeanUtils.copyProperties(path, pathVO);

        PathPoint pathPoint = new PathPoint();
        pathPoint.setUpperId(id);
        List<PathPoint> points = pathPointMapper.selectPathPointList(pathPoint);
        pathVO.setPoints(points);

        List<List<BigDecimal>> locations = new ArrayList<>();
        List<Map> locationMap = new ArrayList<>();
        if (points != null) {
            for (PathPoint point : points) {
                List<BigDecimal> location = new ArrayList<>();
                location.add(point.getLongitude());
                location.add(point.getLatitude());
                locations.add(location);

                Map map = new HashMap();
                map.put("lat", point.getLatitude());
                map.put("lon", point.getLongitude());
                locationMap.add(map);
            }
        }
        pathVO.setLocations(locations);
        pathVO.setLocationMap(locationMap);
        return pathVO;
    }

    /**
     * 查询应急路径列表
     * 
     * @param emergencyPath 应急路径
     * @return 应急路径
     */
    @Override
    public List<EmergencyPath> selectEmergencyPathList(EmergencyPath emergencyPath)
    {
        return emergencyPathMapper.selectEmergencyPathList(emergencyPath);
    }

    /**
     * 新增应急路径
     * 
     * @param emergencyPath 应急路径
     * @return 结果
     */
    @Override
    public int insertEmergencyPath(EmergencyPath emergencyPath)
    {
        String token = SecurityUtils.getToken();
        String userKey = CacheConstants.LOGIN_TOKEN_KEY + token;
        LoginUser user = redisService.getCacheObject(userKey);

        emergencyPath.setCreateUserId(user.getUserid());
        emergencyPath.setCreateBy(user.getUsername());
        emergencyPath.setCreateTime(DateUtils.getNowDate());
        return emergencyPathMapper.insertEmergencyPath(emergencyPath);
    }

    /**
     * 修改应急路径
     * 
     * @param emergencyPath 应急路径
     * @return 结果
     */
    @Override
    public int updateEmergencyPath(EmergencyPath emergencyPath)
    {
        String token = SecurityUtils.getToken();
        String userKey = CacheConstants.LOGIN_TOKEN_KEY + token;
        LoginUser user = redisService.getCacheObject(userKey);

        emergencyPath.setUpdateBy(user.getUsername());
        emergencyPath.setUpdateTime(DateUtils.getNowDate());
        return emergencyPathMapper.updateEmergencyPath(emergencyPath);
    }

    /**
     * 批量删除应急路径
     * 
     * @param ids 需要删除的应急路径主键
     * @return 结果
     */
    @Override
    public int deleteEmergencyPathByIds(String[] ids)
    {
        return emergencyPathMapper.deleteEmergencyPathByIds(ids);
    }

    /**
     * 删除应急路径信息
     * 
     * @param id 应急路径主键
     * @return 结果
     */
    @Override
    public int deleteEmergencyPathById(Long id)
    {
        return emergencyPathMapper.deleteEmergencyPathById(id);
    }

    @Override
    public List<EmergencyPath> findByUpperIds(List<Long> upperIds) {
        return emergencyPathMapper.findByUpperIds(upperIds);
    }
}
